package neuronvisualizer;

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.threed.jpct.TextureManager;

public class OptionBar extends JFrame implements ComponentListener, ActionListener{
	private Driver parent;
	private Visual parent2;
	
	private final Rectangle FRAMESIZE = new Rectangle(10,20,220,700);
	
	private JCheckBox[] showTypes = {new JCheckBox("RS",true),new JCheckBox("LTS",true),new JCheckBox("FS",true),new JCheckBox("IB",true)};
	private JCheckBox[] showLayers = {new JCheckBox("II/III",true),new JCheckBox("IV",true),new JCheckBox("V",true),new JCheckBox("VI",true)};

	private JButton chooseNeuronFile;
	private JButton chooseSpikeFile;

	private JButton cameraOptions;
	private JButton highlightOptions;
	private JButton networkOptions;
	private JButton helpOptions;
	private JButton rotateOptions;
	private JButton dataOptions;
	private JButton sectionButton;
	
	private JPanel cameraOptionPanel;
	private JPanel highlightOptionPanel;
	private JPanel networkOptionPanel;	
	private JPanel rotateOptionsPanel;
	private JPanel helpOptionPanel;
	private JPanel dataOptionPanel;
	private SectionGUI sectionPanel;
	
	private JLabel currentPanel;
	
	private final int BUTTONWIDTH = 170;
	private final int BUTTONHEIGHT = 40;
	private final int CONNECTMAX = 100;
	private final int CONNECTMIN = 0;
	public static final int CONNECTINIT = 20;
	
	public OptionBar(Driver p){
		super("Options");
		parent = p;
		try {
		    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
		        if ("Nimbus".equals(info.getName())) {
		            UIManager.setLookAndFeel(info.getClassName());
		            break;
		        }
		    }
		} catch (Exception e) {
			try {
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			} catch (Exception E){
				E.printStackTrace();
			}
		}
		setupGUI();
		
		this.setVisible(true);
        
	}
	
	public void setupGUI(){
	    this.setBounds(FRAMESIZE);
		this.setResizable(false);
		this.setLayout(null);
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		this.addComponentListener(this);
		
		chooseNeuronFile = new JButton("Load Neurons",createImageIcon("resource/icons/txt.png"));
		chooseNeuronFile.addActionListener(new FileButtonListener(this));
		chooseNeuronFile.setBounds(20,10,170,40);
		chooseNeuronFile.setHorizontalAlignment(SwingConstants.CENTER);
		
		chooseSpikeFile = new JButton("Load Spikes",createImageIcon("resource/icons/txt.png"));
		chooseSpikeFile.setBounds(20,50,170,40);
		chooseSpikeFile.setEnabled(false);
		chooseSpikeFile.addActionListener(new TimelineButtonListener(this));
		chooseSpikeFile.setHorizontalAlignment(SwingConstants.CENTER);
		
		cameraOptions = new JButton("Camera Options",createImageIcon("resource/icons/camera.png"));
		cameraOptions.setBounds(20,90,170,40);
		cameraOptions.setEnabled(false);
		cameraOptions.addActionListener(this);
		cameraOptions.setHorizontalAlignment(SwingConstants.CENTER);
		
		highlightOptions = new JButton("Highlight Options");
		highlightOptions.setBounds(20,130,170,40);
		highlightOptions.setEnabled(false);
		highlightOptions.addActionListener(this);
		highlightOptions.setHorizontalAlignment(SwingConstants.CENTER);
		
		networkOptions = new JButton("Network Options",createImageIcon("resource/icons/neurons.png"));
		networkOptions.setBounds(20,170,170,40);
		networkOptions.setEnabled(false);
		networkOptions.addActionListener(this);
		networkOptions.setHorizontalAlignment(SwingConstants.CENTER);
		
		dataOptions = new JButton("Data Options");
		dataOptions.setBounds(20,210,170,40);
		dataOptions.setEnabled(false);
		dataOptions.addActionListener(this);
		dataOptions.setHorizontalAlignment(SwingConstants.CENTER);
		
		sectionButton = new JButton("Sections");
		sectionButton.setBounds(20,250,BUTTONWIDTH,BUTTONHEIGHT);
		sectionButton.setEnabled(false);
		sectionButton.addActionListener(this);
		
		helpOptions = new JButton("Help Options",createImageIcon("resource/icons/qmark.png"));
		helpOptions.setBounds(20,290,170,40);
		helpOptions.setEnabled(false);
		helpOptions.addActionListener(this);
		helpOptions.setHorizontalAlignment(SwingConstants.CENTER);
		
		currentPanel = new JLabel("");
		currentPanel.setBounds(20,320,170,30);
				
		networkOptionPanel = createNetworkOptionPanel();
		cameraOptionPanel = createCameraOptionPanel();
		highlightOptionPanel = createHighlightOptionPanel();
		helpOptionPanel = createHelpOptionPanel();
		sectionPanel = new SectionGUI(this.parent);
			sectionPanel.setBounds(20,350,170,250);
			sectionPanel.setVisible(false);
		dataOptionPanel = createDataOptionPanel();
		
		add(chooseNeuronFile);
		add(chooseSpikeFile);
		
		add(cameraOptions);
		add(highlightOptions);
		add(networkOptions);
		add(sectionButton);
		add(helpOptions);
		add(dataOptions);
		
		add(currentPanel);
		
		add(cameraOptionPanel);
		add(networkOptionPanel);
		add(highlightOptionPanel);
		add(helpOptionPanel);
		add(sectionPanel);
		add(dataOptionPanel);

	}
	
	public void resetSections(){
		sectionPanel.reset();
	}
	
	public JPanel createDataOptionPanel(){
		JPanel temp = new JPanel();
		temp.setBounds(20,350,170,250);
		temp.setLayout(null);
		
		JButton showAnalysisBar = new JButton("Toggle Analysis");
		showAnalysisBar.setBounds(0,10,150,40);
		showAnalysisBar.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent e){
					
					parent.showAnalysis();
					
				}
			}
		);
		temp.add(showAnalysisBar);
		
		JButton outData = new JButton("Output Data");
		outData.setBounds(0,55,150,40);
		outData.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent e){
					DataFrame dataframe = new DataFrame(parent);
				}
			}
		);
		temp.add(outData);
		
		temp.setVisible(false);
		return temp;
	}
	

	public JPanel createNetworkOptionPanel(){
		JPanel temp = new JPanel();
		temp.setBounds(20,350,170,250);
		temp.setLayout(null);
		
		JLabel showT = new JLabel("Show Neuron Type:");
		showT.setBounds(0,0,150,25);
		temp.add(showT);
		
		for(int i = 0 ;i<showTypes.length;i++){
			JCheckBox t = showTypes[i];
			t.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e){
						String tType = ((JCheckBox)e.getSource()).getText();						
						boolean vis = ((JCheckBox)e.getSource()).isSelected();						
						if(tType.equals("RS")){							
							parent.toggleType(neuronvisualizer.Type.RS,vis);
						}
						else if(tType.equals("FS")){
							parent.toggleType(neuronvisualizer.Type.FS,vis);
						}
						else if(tType.equals("LTS")){
							parent.toggleType(neuronvisualizer.Type.LTS,vis);
						}
						else if(tType.equals("IB")){
							parent.toggleType(neuronvisualizer.Type.IB,vis);
						}
					}
				}
			);
			
			t.setBounds(0,30+(i*25),150,26);
			temp.add(t);
		}
		
		JLabel conPercent = new JLabel("Show % of Connections");
		conPercent.setBounds(0,130,150,26);
		temp.add(conPercent);
		
		JSlider slider = new JSlider(JSlider.HORIZONTAL,CONNECTMIN,CONNECTMAX,CONNECTINIT);
		slider.setMajorTickSpacing(10);
		slider.setMinorTickSpacing(1);
		slider.setPaintTicks(true);
		slider.setBounds(0,150,170,50);
		slider.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
        		int threshold = ((JSlider)evt.getSource()).getValue();
        		parent.updateConnectionThreshold(threshold);                
            }
            public void mouseClicked(java.awt.event.MouseEvent evt) {
            }
        });
		temp.add(slider);
		
		
		temp.setVisible(false);
		return temp;
	}
	/*public JPanel createRotateOptionsPanel(){
		JPanel temp = new JPanel();
		temp.setBounds(20,350,170,250);
		temp.setLayout(null);
		
		
		JButton rotateMode = new JButton("Rotate Mode");
		rotateMode.setBounds(0,0,150,40);
		rotateMode.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent arg0) {
						parent2.checkMouseRotate();
					}
				}
		);
		temp.add(rotateMode);

		temp.setVisible(false);
		return temp;
	}*/
	public JPanel createHighlightOptionPanel(){
		JPanel temp = new JPanel();
		temp.setBounds(20,350,170,250);
		temp.setLayout(null);
		
		JLabel pickColumn = new JLabel("Column:");
		pickColumn.setBounds(0,0,100,20);
		
		final JTextField column = new JTextField();
		column.setBounds(0,20,100,25);
		
		JLabel pickLayer = new JLabel("Layer:");
		pickLayer.setBounds(0,45,100,20);
		
		String layers[] = {"II/III","IV","V","VI"};
		final JComboBox layer = new JComboBox(layers);
		layer.setBounds(0,65,100,25);
		
		JLabel pickColor = new JLabel("Color:");
		pickColor.setBounds(0,90,100,20);
		
		String colors[] = new String[TextureManager.getInstance().getTextureCount()-2];
		for(int i=1;i<colors.length+1;i++){
			colors[i-1] = TextureManager.getInstance().getNameByID(i);
		}
		final JComboBox color = new JComboBox(colors);
		color.setBounds(0,110,100,25);
		
		JButton createHighlight = new JButton("Highlight");
		createHighlight.setBounds(0,140,150,40);
		createHighlight.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent arg0){
					parent.highlight(column.getText(),(String)layer.getSelectedItem(),(String)color.getSelectedItem());
				}
			}
		);
		
		JButton clearHighlights = new JButton("Clear Highlights");
		clearHighlights.setBounds(0,185,150,40);
		clearHighlights.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent arg0){
					parent.clearHighlights();
				}
			}
		);
		
		temp.add(pickColumn);
		temp.add(pickLayer);
		temp.add(pickColor);
		
		temp.add(column);
		temp.add(layer);
		temp.add(color);
		
		temp.add(createHighlight);
		temp.add(clearHighlights);
		
		temp.setVisible(false);
		return temp;
	}
	
	public JPanel createCameraOptionPanel(){
		JPanel temp = new JPanel();
		temp.setBounds(20,350,170,250);
		temp.setLayout(null);
		
		JButton screenshotButton = new JButton("Screenshot",createImageIcon("resource/icons/png.png"));
		screenshotButton.setBounds(0,0,150,40);
		screenshotButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent arg0) {
						parent.takeScreenshot();
					}
				}
		);
		temp.add(screenshotButton);
			
		JButton resetCamera = new JButton("Reset Camera");
		resetCamera.setBounds(0,45,150,40);
		resetCamera.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent arg0) {
					parent.resetCamera();
				}
			}
		);
		temp.add(resetCamera);
		
		temp.setVisible(false);
		return temp;
	}
	
	public JPanel createHelpOptionPanel()
	{
		JPanel temp = new JPanel();
		
		temp.setBounds(20,350,170,250);
		temp.setLayout(null);
		
		JLabel RS = new JLabel(": RS Neuron");
		RS.setBounds(25,0,100,20);
		JLabel RSim = new JLabel(createImageIcon("resource/icons/RS.png"));
		RSim.setHorizontalAlignment(SwingConstants.LEFT);
		RSim.setBounds(0,0,100,20);
		
		JLabel LTS = new JLabel(": LTS Neuron");
		LTS.setBounds(25,25,100,25);
		JLabel LTSim = new JLabel(createImageIcon("resource/icons/LTS.jpg"));
		LTSim.setHorizontalAlignment(SwingConstants.LEFT);
		LTSim.setBounds(0,25,100,25);
		
		JLabel FS = new JLabel(": FS Neuron");
		FS.setBounds(25,50,100,30);
		JLabel FSim = new JLabel(createImageIcon("resource/icons/FS.png"));
		FSim.setHorizontalAlignment(SwingConstants.LEFT);
		FSim.setBounds(0,50,100,30);
		
		JLabel IB = new JLabel(": IB Neuron");
		IB.setBounds(25,75,100,35);
		JLabel IBim = new JLabel(createImageIcon("resource/icons/IB.jpg"));
		IBim.setHorizontalAlignment(SwingConstants.LEFT);
		IBim.setBounds(0,75,100,35);
		
		JLabel conn = new JLabel("Connections");
		conn.setBounds(0,95,100,40);
		
		JLabel poscon = new JLabel(": Positive");
		poscon.setBounds(40,110,100,40);
		JLabel posconim = new JLabel(createImageIcon("resource/icons/poscon.png"));
		posconim.setHorizontalAlignment(SwingConstants.LEFT);
		posconim.setBounds(0,110,100,40);
		
		JLabel negcon = new JLabel(": Negative");
		negcon.setBounds(40,130,100,45);
		JLabel negconim = new JLabel(createImageIcon("resource/icons/negcon.png"));
		negconim.setHorizontalAlignment(SwingConstants.LEFT);
		negconim.setBounds(0,130,100,45);
		
		JLabel out = new JLabel("Solid Line : ");
		out.setBounds(0,150,100,50);
		JLabel out1 = new JLabel(" Outgoing");
		out1.setBounds(60,150,100,50);
		
		JLabel in = new JLabel("Transparent Lin");
		in.setBounds(0,170,100,50);
		JLabel in1 = new JLabel("e : Incoming");
		in1.setBounds(87,170,100,50);
		
		JLabel rotateh = new JLabel("To rotate use ");
		rotateh.setBounds(0,200,100,50);
		JLabel rotateh1 = new JLabel("arrow keys ");
		rotateh1.setBounds(75,200,100,50);
		JLabel zoom = new JLabel("To zoom scroll");
		zoom.setBounds(0,220,100,50);
		JLabel zoom1 = new JLabel(" mouse");
		zoom1.setBounds(80,220,100,50);
		
		temp.add(RS);
		temp.add(RSim);
		temp.add(LTS);
		temp.add(LTSim);
		temp.add(FS);
		temp.add(FSim);
		temp.add(IB);
		temp.add(IBim);
		
		temp.add(conn);
		temp.add(poscon);
		temp.add(posconim);
		temp.add(negcon);
		temp.add(negconim);
		temp.add(rotateh); temp.add(rotateh1); temp.add(zoom); temp.add(zoom1);
		temp.add(out); temp.add(out1);
		temp.add(in); temp.add(in1);
		
		temp.setVisible(false);
		return temp;
	}
	
	public void enableOptions(){
		chooseSpikeFile.setEnabled(true);
		highlightOptions.setEnabled(true);
		networkOptions.setEnabled(true);
		cameraOptions.setEnabled(true);	
		helpOptions.setEnabled(true);
		//rotateOptions.setEnabled(true);
		sectionButton.setEnabled(true);
		dataOptions.setEnabled(true);
	}
	
	public ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = this.parent.getClass().getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
	}

	public void componentHidden(ComponentEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void componentMoved(ComponentEvent arg0) {
				
	}

	public void componentResized(ComponentEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void componentShown(ComponentEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	public Driver getDriver(){
		return parent;
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==cameraOptions){
			highlightOptionPanel.setVisible(false);
			networkOptionPanel.setVisible(false);
			cameraOptionPanel.setVisible(true);	
			dataOptionPanel.setVisible(false);
			helpOptionPanel.setVisible(false);
			sectionPanel.setVisible(false);
			//rotateOptionsPanel.setVisible(false);
			currentPanel.setText("Camera Options");
		}
		else if(e.getSource()==sectionButton){
			highlightOptionPanel.setVisible(false);
			networkOptionPanel.setVisible(false);
			cameraOptionPanel.setVisible(false);	
			dataOptionPanel.setVisible(false);
			helpOptionPanel.setVisible(false);
			//rotateOptionsPanel.setVisible(false);
			currentPanel.setText("Section Options");
			sectionPanel.setVisible(true);
		}
		else if(e.getSource()==networkOptions){
			highlightOptionPanel.setVisible(false);
			cameraOptionPanel.setVisible(false);
			networkOptionPanel.setVisible(true);
			dataOptionPanel.setVisible(false);
			helpOptionPanel.setVisible(false);
			sectionPanel.setVisible(false);
			//rotateOptionsPanel.setVisible(false);
			currentPanel.setText("Network Options");
		}
		else if(e.getSource() == highlightOptions){
			cameraOptionPanel.setVisible(false);
			networkOptionPanel.setVisible(false);
			highlightOptionPanel.setVisible(true);
			helpOptionPanel.setVisible(false);
			sectionPanel.setVisible(false);
			dataOptionPanel.setVisible(false);
			//rotateOptionsPanel.setVisible(false);
			currentPanel.setText("Highlight Options");
		}
		else if(e.getSource() == helpOptions){
			cameraOptionPanel.setVisible(false);
			networkOptionPanel.setVisible(false);
			highlightOptionPanel.setVisible(false);
			helpOptionPanel.setVisible(true);
			sectionPanel.setVisible(false);
			dataOptionPanel.setVisible(false);
			//rotateOptionsPanel.setVisible(false);
			currentPanel.setText("Help Options");
		}
		/*else if(e.getSource() == rotateOptions){
			cameraOptionPanel.setVisible(false);
			networkOptionPanel.setVisible(false);
			highlightOptionPanel.setVisible(false);
			helpOptionPanel.setVisible(false);
			//rotateOptionsPanel.setVisible(true);
			currentPanel.setText("Rotate Options");
		}*/

		else if(e.getSource() == dataOptions){
			cameraOptionPanel.setVisible(false);
			networkOptionPanel.setVisible(false);
			sectionPanel.setVisible(false);
			highlightOptionPanel.setVisible(false);
			helpOptionPanel.setVisible(false);
			dataOptionPanel.setVisible(true);
			currentPanel.setText("Data Options");
		}
	}

	public void stateChanged(ChangeEvent arg0) {
		JSlider source = (JSlider)arg0.getSource();
		int threshold = source.getValue();
		parent.updateConnectionThreshold(threshold);

	}

}
